package cn.com.blueInfo.HaiYuan.myWorks.service.impl;

import cn.com.blueInfo.HaiYuan.myWorks.service.ApplyService;
//import cn.com.blueInfo.bpm.manager.power.entity.Navigation;
//import cn.com.blueInfo.bpm.manager.power.entity.Role;
//import cn.com.blueInfo.bpm.manager.power.entity.RoleResource;
//import cn.com.blueInfo.bpm.manager.power.mapper.RoleMapper;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 新建页面管理服务接口具体实现
 * @ClassName: ApplyServiceImpl
 * @author xichen.su
 * @date 2019年5月16日  下午12:52:55
 */
@Log4j2
@Service
public class ApplyServiceImpl implements ApplyService {

//    @Autowired
//    private RoleMapper roleMapper;

    /**
     * 通过身份串获取新建项具体实现
     * @Title: queryApplyItemByIdentity
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryApplyItemByIdentity(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            Map<String, Object> paramMap = JSON.parseObject(requestData);
            paramMap.put("typeCode", "ROLE_TYPE_DRAFT");

            String identityStr = paramMap.get("identity").toString();
            String[] identityArr = identityStr.split("#");
            String loginName = identityArr[identityArr.length - 1];
            if (!"admin".equals(loginName)) {
                paramMap.put("loginName", loginName);
                paramMap.put("orgCode", identityArr[2]);
                paramMap.put("headshipCode", identityArr[identityArr.length - 2]);
            }

            try {
//                List<Role> roleList = null;
//                if (!"admin".equals(loginName)) {
//                    roleList = roleMapper.queryApplyNavigationByUser(paramMap);
//                } else {
//                    roleList = roleMapper.queryApplyNavigationByRoot(paramMap);
//                }
//
//                List<Navigation> navList = new ArrayList<Navigation>();
//                for (Role role : roleList) {
//                    List<RoleResource> resourceList = role.getRoleResourceList();
//                    for (RoleResource resource : resourceList) {
//                        navList.addAll(resource.getNavigationList());
//                    }
//                }

                result.setResult(true);
//                result.setData(navList);
                result.setMessage("新建数据获取成功");
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage("新建项数据获取失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
            }
        }

        return result;
    }

}
